def gomoku_game():
    BOARD_SIZE = 15
    board = [[0 for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]
    current_player = 1  # 1为黑棋，2为白棋
    game_over = False   # 游戏结束标志
    
    def print_board():
        for row in board:
            print(' '.join(['.' if cell == 0 else 'X' if cell == 1 else 'O' for cell in row]))
    
    def place_stone(x, y, player):
        if 0 <= x < BOARD_SIZE and 0 <= y < BOARD_SIZE and board[x][y] == 0:
            board[x][y] = player
            return True
        return False
    
    def check_win(x, y, player):
        directions = [(0, 1), (1, 0), (1, 1), (1, -1)]  # 四个方向
        for dx, dy in directions:
            count = 1
            # 正向检查
            i = 1
            while True:
                nx, ny = x + i * dx, y + i * dy
                if 0 <= nx < BOARD_SIZE and 0 <= ny < BOARD_SIZE and board[nx][ny] == player:
                    count += 1
                    i += 1
                else:
                    break
            # 反向检查
            i = 1
            while True:
                nx, ny = x - i * dx, y - i * dy
                if 0 <= nx < BOARD_SIZE and 0 <= ny < BOARD_SIZE and board[nx][ny] == player:
                    count += 1
                    i += 1
                else:
                    break
            if count >= 5:
                return True
        return False
    
    # 游戏主循环（使用while）
    while not game_over:
        print_board()
        print(f"玩家{current_player}落子（输入格式：x y）：")
        
        # 输入处理循环（确保输入合法）
        input_valid = False
        while not input_valid:
            try:
                x, y = map(int, input().split())
                if place_stone(x, y, current_player):
                    input_valid = True  # 输入合法，退出输入循环
                else:
                    print("落子位置不合法，请重新输入！")
            except ValueError:
                print("输入格式错误，请输入两个整数（例如：7 7）！")
        
        # 检查胜负
        if check_win(x, y, current_player):
            print_board()
            print(f"玩家{current_player}获胜！")
            game_over = True  # 游戏结束
        else:
            # 切换玩家
            current_player = 2 if current_player == 1 else 1

if __name__ == "__main__":
    gomoku_game()